/*******************************************************************************
 * Copyright 2007, 2009 InnovaSoft SA http://www.innovasoft.biz/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
 
package biz.innovasoft.nodb;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.prevayler.foundation.serialization.GZIPSerializer;
import org.prevayler.foundation.serialization.XStreamSerializer;

/**
 * @author diego
 *
 * @param <K>
 * @param <V>
 */
public class CompressableHashtable<K,V> extends Hashtable<K,V> implements Serializable {

	private static final long serialVersionUID = 1L;
 
	private transient GZIPSerializer gzipserializer;
	private boolean compressed = false;

	public CompressableHashtable() {
		super(16);
		validateSerializer();
	}
	
	public CompressableHashtable (boolean compressed) {
		super(16);
		this.compressed = compressed;
		validateSerializer();
	}

	protected void validateSerializer () {
		if (gzipserializer == null) {
			XStreamSerializer xstream = new XStreamSerializer();
			gzipserializer = new GZIPSerializer (xstream, 2048);
		}
	}
	
	@SuppressWarnings("unchecked")
	public V put(K key, V object) {
		if (compressed) {
			//validateSerializer();
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream ();
				gzipserializer.writeObject(baos, object);
				return super.put(key, (V)baos.toByteArray());
			} catch (Exception e) {
				throw new RuntimeException (e);
			}
		}
		return super.put(key, object);
	}
	
	@SuppressWarnings("unchecked")
	public V get(Object key) {
		if (compressed) {
			//validateSerializer();
			try {
				byte []bytes = (byte[])super.get(key);
				ByteArrayInputStream bais = new ByteArrayInputStream (bytes);
				Object value = gzipserializer.readObject(bais);
				return (V)value;
			} catch (Exception e) {
				throw new RuntimeException (e);
			}
		}
		return super.get(key);
	}

	@SuppressWarnings("unchecked")
	@Override
	public synchronized boolean contains(Object object) {
		if (compressed) {
			validateSerializer();
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream ();
				gzipserializer.writeObject(baos, object);
				return super.contains((V)baos.toByteArray());
			} catch (Exception e) {
				throw new RuntimeException (e);
			}
		}
		return super.contains(object);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean containsValue(Object object) {
		if (compressed) {
			validateSerializer();
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream ();
				gzipserializer.writeObject(baos, object);
				return super.containsValue((V)baos.toByteArray());
			} catch (Exception e) {
				throw new RuntimeException (e);
			}
		}
		return super.containsValue(object);
	}

	@Override
	public synchronized Enumeration<V> elements() {
		if(compressed) {
		}
		return super.elements();
	}

	@Override
	public Set<java.util.Map.Entry<K, V>> entrySet() {
		if(compressed) {
		}
		return super.entrySet();
	}

	@Override
	public synchronized void putAll(Map<? extends K, ? extends V> map) {
		if (map instanceof CompressableHashtable) {
			if (compressed) {
				super.putAll(map);
			}
		}
		for (Map.Entry<? extends K, ? extends V> e : map.entrySet())
            put(e.getKey(), e.getValue());
	}

	public boolean isCompressed () {
		return compressed;
	}
	
}
